from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import os


class AlertOperation:
    def __init__(self):
        # 打开浏览器
        self.driver = webdriver.Chrome()
        # 动态获取地址
        self.url = 'file:///' + os.path.abspath('html') + '/注册实例.html'
        self.driver.get(self.url)
        # 最大化
        self.driver.maximize_window()

    def frist_alert(self):
        time.sleep(2)
        # 点击alert按钮
        self.driver.find_element(By.ID, 'alert').click()
        time.sleep(2)
        # 点击确定
        self.driver.switch_to.alert.accept()

    def second_alert(self):
        # 切换frame页面
        self.driver.switch_to.frame('idframe1')
        time.sleep(2)
        # 点击alert按钮
        self.driver.find_element(By.ID, 'alerta').click()
        time.sleep(2)
        # 点击确定
        self.driver.switch_to.alert.accept()

    def third_alert(self):
         # 返回最上层frame页面
        self.driver.switch_to.default_content()
         # 切换frame页面
        f3 = self.driver.find_element(By.XPATH, '/html/body/s/iframe[2]')
        self.driver.switch_to.frame(f3)
        time.sleep(2)
         # 点击alert按钮
        self.driver.find_element(By.ID, 'alertB').click()
        time.sleep(2)
         # 点击确定
        self.driver.switch_to.alert.accept()

    def quit(self):
        time.sleep(2)
        # 退出浏览器
        self.driver.quit()


if __name__ == '__main__':
    a1 = AlertOperation()
    a1.frist_alert()
    a1.second_alert()
    a1.third_alert()
    a1.quit()
